package com.hb.znpt.viewmodel;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.hb.znpt.activity.LandAddActivity;
import com.hb.znpt.bean.LandBean;
import com.hb.znpt.bean.LesseeBean;
import com.hb.znpt.model.LandModel;
import com.rxjava.rxlife.RxLife;
import com.unitlib.base.base.BaseViewModel;
import com.unitlib.base.utils.ToastUtils;
import com.xuexiang.xutil.net.NetworkUtils;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2021/12/17 0:20
 */
public class LandViewModel extends BaseViewModel<LandModel> {

    public LesseeBean dept;

    public MutableLiveData<List<LandBean>> listData = new MutableLiveData<>();

    public LandViewModel(@NonNull Application application) {
        super(application);
        model = new LandModel();
    }

    @Override
    public void onCreate() {
        dept = (LesseeBean) intent.getSerializableExtra("data");
    }

    public void add(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("deptid", dept.getDeptid());
        startActivity(LandAddActivity.class, bundle);
    }

    public void getData() {
        if (NetworkUtils.isHaveInternet()) {
            model.getList(dept.getDeptid())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(disposable -> {
                        getUC().getShowLoadingDialogEvent().call();
                    })
                    .doFinally(() -> {
                        getUC().getDismissLoadingDialogEvent().call();
                    })
                    .as(RxLife.as(owner))
                    .subscribe(beans -> {
                        if (model.getOffDataCount(dept.getDeptid()) == 0) {
                            for (int i = 0; i < beans.size(); i++) {
                                beans.get(i).setLessee(dept);
                                beans.get(i).setDeptid(dept.getDeptid());
                                beans.get(i).save();
                            }
                        } else {
                            if (beans.size() == 0) {
                                LitePal.deleteAll(LandBean.class, "deptid = ?", dept.getDeptid());
                            } else {
                                for (int i = 0; i < beans.size(); i++) {
                                    beans.get(i).setLessee(dept);
                                    beans.get(i).setDeptid(dept.getDeptid());
                                    beans.get(i).saveOrUpdate("zwid = ?", beans.get(i).getZwid());
                                }
                                List<LandBean> list = model.getOffData(dept.getDeptid());
                                for (int i = 0; i < list.size(); i++) {
                                    boolean isHave = false;
                                    for (int j = 0; j < beans.size(); j++) {
                                        if (list.get(i).getZwid().equals(beans.get(j).getZwid())) {
                                            isHave = true;
                                        }
                                    }
                                    if (!isHave) {
                                        list.get(i).delete();
                                    }
                                }
                            }
                        }
                        listData.setValue(beans);
                    }, error -> {
                        if (error.getLocalizedMessage().equals("000006")) {
                            LitePal.deleteAll(LandBean.class, "deptid = ?", dept.getDeptid());
                        } else {
                            ToastUtils.showShort(error.getMessage());
                            listData.setValue(model.getOffData(dept.getDeptid()));
                        }
                    });
        } else {
            listData.setValue(model.getOffData(dept.getDeptid()));
        }
    }
}
